/**
 * LocalVariablesManager.java  
 *        
 * Linker is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package linker;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Properties;

import linker.account.ManageAccount;

/**
 * This class will handle all local variables including all paths and string
 * constances. We save all the paths and string constances in a file as
 * properties and use the static method getLocalVariables to get the variable
 * you need.
 * 
 * I'm not sure how should we organize the structure of all local variables.
 * Temporarily we pick up all visual information and save them locally.
 * 
 * 
 */

public class LocalVariablesManager extends Object {

	/**
	 * The translated variables.
	 */
	private static Properties localVariables;

	/**
	 * The saved settings.
	 */
	private static Properties settings;

	/**
	 * The self refer.
	 */
	private static LocalVariablesManager manager = null;

	/**
	 * LocalVariablesManager's constructor.
	 * 
	 */
	public LocalVariablesManager() {
		localVariables = new Properties();
		settings = new Properties();
		File file = new File(ManageAccount.getConfigPath() + "settings.ini");

		try {
			settings.load(new InputStreamReader(new FileInputStream(file),
					"UTF-8"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		file = new File(ManageAccount.getConfigPath() + getSettings("Language")
				+ ".lng");
		try {
			localVariables.load(new InputStreamReader(
					new FileInputStream(file), "UTF-8"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Get localVariables.
	 * 
	 * @param variableName
	 *            the name of variable
	 * @return the value
	 */
	public static String getLocalVariables(final String variableName) {
		if (manager == null) {
			manager = new LocalVariablesManager();
		}

		return localVariables.getProperty(variableName);
	}
	/**
	 * Get settings value by setting name.
	 * 
	 * @param settingName
	 *            The name of setting value
	 * @return the SettingValue
	 */
	public static String getSettings(final String settingName) {
		return settings.getProperty(settingName);
	}

	/**
	 * Save setting include its name and value.
	 * 
	 * @param name
	 *            The name of setting
	 * @param value
	 *            The value of setting
	 */
	public static void setSettings(final String name, final String value) {
		settings.put(name, value);
		try {
			settings.store(new OutputStreamWriter(new FileOutputStream(
					ManageAccount.getConfigPath() + "settings.ini"), "UTF-8"),
					"");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
